require "watir-webdriver"
include Watir
require "rautomation"
require "securerandom"
require File.dirname(__FILE__) + '/Xls.rb'

#Open browser
test_site = 'http://gardencity.morbo.i.patch.com/'
browser = Watir::Browser.new :ff
browser.goto(test_site)

#Maximize browser
screen_width = browser.execute_script("return screen.width;")
screen_height = browser.execute_script("return screen.height;")
browser.driver.manage.window.resize_to(screen_width,screen_height)
browser.driver.manage.window.move_to(0,0)

#Click Post button
browser.button(:class => "dropdown-toggle icon_post-menu-button").click

#Click Note
browser.a(:href => "/posts/note/new").fire_event("onclick")
sleep 4

#Go to Note page
if browser.url.include?("http://gardencity.morbo.i.patch.com/login?redirect=%2Fposts%2Fnote%2Fnew")
  #Login
  browser.text_field(:name=>"email").set("shamiul.mowla@patch.com") #Input username
  browser.text_field(:name=>"password").set("password") #Input password
  browser.button(:id=>"signin").click #Click Sign In
  #Click Pick a group
  browser.div(:class=>"post-bucket controls").a(:text=>/Pick a group/).fire_event("onclick")
  sleep 4
  #Select random group
  #browser.select_list(:name, "group-chooser-select").fire_event("onclick")
  #length = browser.select_list(:name, "group-chooser-select").options.length
  #random_value = Kernel.rand(length)
  #browser.select_list(:name, "group-chooser-select").options[random_value].select #Select random group
  #Wait.until {browser.div(:class, "group-chooser-list").exists?}

  repeat = 0
 random_value = 10
  while repeat == 0 do

  browser.select_list(:name, "group-chooser-select").fire_event("onclick")
  length = browser.select_list(:name, "group-chooser-select").options.length
 # random_value = Kernel.rand(length)

  browser.select_list(:name, "group-chooser-select").options[random_value].select #Select random group
  Wait.until {browser.div(:class, "group-chooser-list").exists?}

  repeat = browser.div(:class, "group-chooser-list").divs.length
  if repeat > 0
    group_item = browser.div(:class, "group-chooser-list").divs.length
    random_item = Kernel.rand(group_item)
    browser.div(:class, "group-chooser-list").divs[random_item].fire_event("onclick")
    sleep 4
  end

  random_value = Kernel.rand(length)

  end

  #if browser.div(:class, "group-chooser-list").divs.length == 0 #If no item of selected group
  #  browser.select_list(:name, "group-chooser-select").fire_event("onclick")
  #  length = browser.select_list(:name, "group-chooser-select").options.length
  #  random_value = Kernel.rand(length)
  #  browser.select_list(:name, "group-chooser-select").options[random_value].select #Select random group
  #  Wait.until {browser.div(:class, "group-chooser-list").exists?}
  #  group_item = browser.div(:class, "group-chooser-list").divs.length
  #  random_item = Kernel.rand(group_item)
  #  browser.div(:class, "group-chooser-list").divs[random_item].fire_event("onclick")
  #  sleep 4
  #else
  #  group_item = browser.div(:class, "group-chooser-list").divs.length
  #  random_item = Kernel.rand(group_item)
  #  browser.div(:class, "group-chooser-list").divs[random_item].fire_event("onclick")
  #  sleep 4
  #end

  #Compose Note
  #--Read data from source file
  xlFile = XLS.new(File.dirname(__FILE__) + '/data/data_file.xlsx') #Read data from excel file
  myData = xlFile.getRowRecords('A1:B6', 'data_Note') #pull data records  from excel
  xlFile.close
  #--Fill random data to Compose Note field
  note = myData.length
  random_row = Kernel.rand(note)
  row = random_row.to_s
  myData.each do |record|
    if record['#'] == row
      browser.textarea(:id, /post_short_body/).set(record['Content'])
    end
  end

  #Add photo
  #--Click on Add photos or videos link
  browser.link(:text=>"Add photos or videos").click
  sleep 4
  #--Upload file
  win = RAutomation::Window.new(:title => /File Upload/)
  #file_path = "C:\\Users\\.321\\Gardencity\\image\\"
  #random_file = "009.jpg"

  arrFILES = []
  file_path = File.dirname(__FILE__) + "/image/"
  Dir.foreach(file_path) do  |f|
    #|f| files.push(f) if File.file?(f)
    if f != '..' && f!= '.'
      arrFILES<<f
    end
  end
  file = arrFILES.length
  random_file = Kernel.rand(file)
  filename = arrFILES[random_file]
  fileupload = file_path + filename
  puts fileupload.gsub("\\","/")
  win.text_field.set(fileupload.gsub("/","\\"))
  win.button(:value=>"&Open").click

  #Click Post Now button
  browser.button(:id=>"post_submit").click

  #Validate the post
  if browser.link(:text=>"Edit this Post").exists?
    puts ("Post is submited successfully.")
  else
    puts ("Post is not submited successfully.")
  end
end